//
//  SelectYearsView.m
//  JiuLe
//
//  Created by 黄鑫 on 16/10/11.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "SelectYearsView.h"
@interface SelectYearsView ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong) NSMutableArray *selectedArray;
@property (nonatomic, strong) UIToolbar *toolBar;
@property (nonatomic, strong) UIPickerView *pickerView;

@end

@implementation SelectYearsView

- (instancetype)init:(selectYearsBlock)block{
    Myblock = block;
    self = [super init];
    if (self) {
        self.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        [self addSubview:self.pickerView];
        [self addSubview:self.toolBar];
    }
    return self;
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.selectedArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)componen{
    NSNumber *num = _selectedArray[row];
    NSString *str = [NSString stringWithFormat:@"%@",num];
    return str;
}


#pragma mark - UIPickerViewDelegate
//设置列里边组件的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return [UIScreen mainScreen].bounds.size.width;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSNumber *num = _selectedArray[row];
    self.selecrStr = [NSString stringWithFormat:@"%@",num];
}

#pragma mark -set/ger
- (NSMutableArray *)selectedArray{
    if (!_selectedArray) {
        _selectedArray = [@[] mutableCopy];
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"yyyy-MM-dd"];
        NSDate *currentDate = [format dateFromString:@"2008-01-01"];
        NSString *strYear = [format stringFromDate:currentDate];
        int year = [[strYear componentsSeparatedByString:@"-"].firstObject intValue];
        for (int i = 0; i < 98; i++) {
            [_selectedArray addObject:[NSNumber numberWithInt:year-i]];
        }
    }
    return _selectedArray;
}

- (UIPickerView *)pickerView{
    if (_pickerView == nil) {
        self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 250, [UIScreen mainScreen].bounds.size.width, 250)];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        _pickerView.backgroundColor = [UIColor whiteColor];
    }
    return _pickerView;
}

- (UIToolbar *)toolBar{
    if (_toolBar == nil) {
        _toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 294, [UIScreen mainScreen].bounds.size.width, 44)];
        _toolBar.barTintColor=[UIColor whiteColor];
        UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
        item.tag = 120;
        UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
        item2.tag = 121;
        _toolBar.items = @[item,item1, item2];
    }
    return _toolBar;
}

- (void)click:(UIBarButtonItem*)sender
{
    if (sender.tag == 120){
        [self cancelPicker];
    }
    if (sender.tag == 121){
        NSString *address = [NSString stringWithFormat:@"%@",self.selecrStr];
        if ([address isEqualToString:@"(null)"]) {
                address =  @"2008";
        }
        if (Myblock){
            Myblock(address);
        }
        [self cancelPicker];
        
    }
}

- (void)cancelPicker{
    [UIView animateWithDuration:.35 animations:^{
        self.transform = CGAffineTransformMakeScale(1.3, 1.3);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}


@end
